package com.tgit.sso.core.util;

import com.tgit.sso.core.conf.SsoAccountInfoStore;
import com.tgit.sso.core.conf.SsoConf;
import com.tgit.sso.core.conf.SsoLocaleStore;
import com.tgit.sso.core.conf.SsoUser;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 
 * @author 范再军
 * 2018年9月12日
 */
public class SsoLoginHandlerUtils {


    /**
     * get sessionid by cookie (web-cookie)
     *
     * @param request
     * @return
     */
    public static String getSessionIdByCookie(HttpServletRequest request) {
        String cookieSessionId = CookieUtil.getValue(request, SsoConf.SSO_SESSIONID);
        return cookieSessionId;
    }

    /**
     * set sessionid in cookie (web-cookie)
     *
     * @param response
     * @param sessionId
     */
    public static void setSessionIdInCookie(HttpServletResponse response, String sessionId) {
        if (sessionId!=null && sessionId.trim().length()>0) {
            CookieUtil.set(response, SsoConf.SSO_SESSIONID, sessionId, false);
        }
    }

    /**
     * remove sessionId in cookie (web-cookie)
     *
     * @param request
     * @param response
     */
    public static void removeSessionIdInCookie(HttpServletRequest request, HttpServletResponse response) {
        CookieUtil.remove(request, response, SsoConf.SSO_SESSIONID);
    }

    /**
     * load header token (restful-token)
     *
     * @param request
     * @return
     */
    public static String getByHeaderToken(HttpServletRequest request) {
        String token = request.getHeader(SsoConf.SSO_TOKEN);
        return token;
    }

    /**
     * login check
     *
     * @param request
     * @return
     */
    public static SsoUser loginCheck(HttpServletRequest request){
        String cookieSessionId = getSessionIdByCookie(request);
        if (cookieSessionId!=null && cookieSessionId.trim().length()>0) {
            return loginCheck(cookieSessionId);
        }
        return null;
    }

    /**
     * login check
     *
     * @param sessionId
     * @return
     */
    public static SsoUser loginCheck(String  sessionId){
        if (sessionId!=null && sessionId.trim().length()>0) {
            SsoUser ssoUesr = SsoAccountInfoStore.get(sessionId);
            if (ssoUesr != null) {
                return ssoUesr;
            }
        }
        return null;
    }

    /**
     * locale check
     *
     * @param sessionId
     * @return
     */
    public static String localeCheck(String  sessionId){
        if (sessionId!=null && sessionId.trim().length()>0) {
            return SsoLocaleStore.get(sessionId);
        }
        return null;
    }

    /**
     * client login (web-cookie)
     *
     * @param response
     * @param sessionId
     * @param ssoUesr
     */
    public static void login(HttpServletResponse response,
                             String sessionId,
                             SsoUser ssoUesr,
                             String ssoLocale) {

        SsoAccountInfoStore.put(sessionId, ssoUesr);
        SsoLocaleStore.put(sessionId, ssoLocale);
        CookieUtil.set(response, SsoConf.SSO_SESSIONID, sessionId, false);
    }

    /**
     * client login (restful-token)
     *
     * @param token
     * @param ssoUesr
     */
    public static void login(String token,
                             SsoUser ssoUesr) {
        SsoAccountInfoStore.put(token, ssoUesr);
    }


    /**
     * client logout (web-cookie)
     *
     * @param request
     * @param response
     */
    public static void logout(HttpServletRequest request,
                              HttpServletResponse response) {

        String cookieSessionId = getSessionIdByCookie(request);

        if (cookieSessionId != null) {
            SsoAccountInfoStore.remove(cookieSessionId);
            SsoLocaleStore.remove(cookieSessionId);
        }
        CookieUtil.remove(request, response, SsoConf.SSO_SESSIONID);
    }

    /**
     * client logout (restful-token)
     *
     * @param token
     */
    public static void logout(String token) {
        SsoAccountInfoStore.remove(token);
    }

}
